package com.ize.controller.admin;

import com.ize.entity.dto.UpdateArticleDto;
import com.ize.entity.pojo.Article;
import com.ize.entity.dto.addArticleDto;
import com.ize.entity.pojo.PageBean;
import com.ize.result.Result;
import com.ize.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/article")
@Slf4j
@Api(tags = "文章通知相关接口", value = "管理员相关接口")
@RequiredArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    //分页查询文档
    @GetMapping
    @ApiOperation("分页查询")
    public Result<PageBean<Article>> list(Integer pageNum,
                                          Integer pageSize,
                                          @RequestParam(required = false) String state,
                                          @RequestParam(required = false) String key
    ){
        log.info("pageNum:{},pageSize:{},state:{},key:{}", pageNum, pageSize, state,key);
        PageBean<Article> pb = articleService.SelectArticle(pageNum,pageSize,state,key);
        log.info("文章返回{}",pb.toString());
        return Result.success(pb);
    }


    @PostMapping
    @ApiOperation("新增通知")
    public Result add(@RequestBody @Validated addArticleDto addArticleDto ){
        log.info("文章{}",addArticleDto.toString());
        articleService.insert(addArticleDto);
        return Result.success();
    }

    /**
     * 修改
     * @param updateArticleDto
     * @return
     */
    @PutMapping
    @ApiOperation("修改文章")
    public Result update(@RequestBody UpdateArticleDto updateArticleDto  ){
        log.info("修改：。。。{}",updateArticleDto.toString());
        articleService.update(updateArticleDto);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除文章")
    public Result delete(@RequestParam Long id){
        log.info("删除文章{}",id);
        articleService.delete(id);
        return Result.success();
    }


    @GetMapping("/key")
    @ApiOperation("搜索文章")
    public Result<PageBean<Article>> key(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String key
    ){
        return Result.success();
    }
}
